package ar.edu.unlam.tpfinal.tests.persistence;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import ar.edu.unlam.tpfinal.model.Address;
import ar.edu.unlam.tpfinal.model.Client;
import ar.edu.unlam.tpfinal.model.Priority;
import ar.edu.unlam.tpfinal.model.Transaction;
import ar.edu.unlam.tpfinal.model.TransactionStatus;
import ar.edu.unlam.tpfinal.model.TransactionType;
import ar.edu.unlam.tpfinal.persistence.dao.AddressDAO;
import ar.edu.unlam.tpfinal.persistence.dao.ClientDAO;
import ar.edu.unlam.tpfinal.persistence.dao.TransactionDAO;
import ar.edu.unlam.tpfinal.persistence.dao.TransactionTypeDAO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:TestClass-context.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
public class TestClass {

	@Autowired
	private AddressDAO addressDAO;
	
	@Autowired
	private ClientDAO clientDAO;
	
	@Autowired
	private TransactionDAO transactionDAO;
	
	@Autowired
	private TransactionTypeDAO transactionTypeDAO;
	
	@Test
	@Transactional
	public void sampleTest() throws ParseException {
		Address direccionCliente1 = new Address();
		direccionCliente1.setCity("Moron");
		direccionCliente1.setCountry("Argentina");
		direccionCliente1.setState("Bs. As.");
		direccionCliente1.setStreet("Azul 1121");
		direccionCliente1.setZipcode("1708");
		addressDAO.save(direccionCliente1);
		Assert.assertTrue(addressDAO.findAll().size()==1);
		
		
		Address direccionCliente2 = new Address();
		direccionCliente2.setCity("Moron");
		direccionCliente2.setCountry("Argentina");
		direccionCliente2.setState("Bs. As.");
		direccionCliente2.setStreet("Azul 1121");
		direccionCliente2.setZipcode("1708");
		addressDAO.save(direccionCliente2);
		Assert.assertTrue(addressDAO.findAll().size()==2);
		
		Client cliente1 = new Client();
		cliente1.setAddresses(direccionCliente1);
		cliente1.setEmail("emailCliente1@hotmail.com");
		cliente1.setFirstName("Cliente 1 Name");
		cliente1.setLastName("Cliente 1 lastName");
		cliente1.setTelephone("1221212");
		clientDAO.save(cliente1);
		Assert.assertTrue(clientDAO.findAll().size()==1);
		
		Client cliente2 = new Client();
		cliente2.setAddresses(direccionCliente2);
		cliente2.setEmail("emailCliente2@hotmail.com");
		cliente2.setFirstName("Cliente 2 Name");
		cliente2.setLastName("Cliente 2 lastName");
		cliente2.setTelephone("2222222");
		clientDAO.save(cliente2);
		Assert.assertTrue(clientDAO.findAll().size()==2);
		
		TransactionType tramiteVentaDeAuto = new TransactionType();
		tramiteVentaDeAuto.setName("Venta de auto");
		tramiteVentaDeAuto.setRequirements("Datos del auto, datos del comprador y vendedor");
		tramiteVentaDeAuto.setComments("08 Firmado, y libre deuda");
		transactionTypeDAO.save(tramiteVentaDeAuto);
		Assert.assertTrue(transactionTypeDAO.findAll().size()==1);
		
		Transaction transaction = new Transaction();
		transaction.setClient(cliente1);
		transaction.setClient(cliente2);
		transaction.setComments("tramite de compra venta de un alfa MiTo");
		transaction.setPriority(Priority.MEDIUM);
		transaction.setFromDate(new Date());
		transaction.setToDate(new SimpleDateFormat("yyyy, MM, dd").parse("2013, 2, 12"));
		transaction.setTransactionType(tramiteVentaDeAuto);
		transaction.setTransactionStatus(TransactionStatus.STARTED);
		transactionDAO.save(transaction);
		Assert.assertTrue(transactionDAO.findAll().size()==1);
		
		System.out.println();
	}
}
